package controllers

import java.io.File
import java.nio.file.Files

import dao.{GroupDao, ModeDao}
import javax.inject.Inject
import play.api.mvc._
import tool.Tool
import utils.Utils

/**
  * Created by yz on 2018/6/11
  */
class Tool @Inject()(modeDao: ModeDao,groupDao: GroupDao) {

  def getRequestDomain(implicit request: RequestHeader) = {
    request.domain
  }

  def getUserId(implicit request: RequestHeader) = {
    request.session.get("id").get.toInt
  }

  def getUserIdDir(implicit request: RequestHeader) = {
    val userId = getUserId
    new File(Tool.dataDir, userId.toString)
  }

  def getProjectIdDir(projectId: Int)(implicit request: RequestHeader) = {
    val userIdDir = getUserIdDir
    new File(userIdDir, projectId.toString)
  }

  def getProjectId(implicit request: RequestHeader) = {
    request.session.get(Tool.projectIdStr).get.toInt
  }

  def getOptionProjectId(implicit request: RequestHeader) = {
    request.session.get(Tool.projectIdStr)
  }

  def getProjectIdDir(implicit request: RequestHeader) = {
    val projectId = getProjectId
    val userIdDir = getUserIdDir
    new File(userIdDir, projectId.toString)
  }

  def getDbFile(implicit request: RequestHeader) = {
    val projectIdDir = getProjectIdDir
    new File(projectIdDir, "data.txt")
  }

  def getDealFile(implicit request: RequestHeader) = {
    val projectIdDir=getProjectIdDir
    new File(projectIdDir, "deal.txt")
  }

  def productGroupFile(groupName: String, tmpDir: File)(implicit request: RequestHeader) = {
    val projectId = getProjectId
    val group = Utils.execFuture(groupDao.selectByGroupName(projectId, groupName)).get
    Utils.productGroupFile(tmpDir, group.content)
  }

  def getUserIdDir(userId:Int)={
    new File(Tool.dataDir,userId.toString)

  }


}
